<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="avatar_box">
                <img src="../assets/img/headPortrait.png" alt="">
            </div>
            <!-- 登陆表单 -->
            <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="80px" class="login_form">
                <el-form-item label="用户名" prop="username">
                    <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.username"></el-input>
                </el-form-item>
                <el-form-item label="用户密码" prop="password">
                    <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" show-password></el-input>
                </el-form-item>
                <!-- 登陆按钮 -->
                <el-form-item class="btns">
                    <el-button type="primary" round @click="login">登陆</el-button>
                    <el-button type="info" round @click="resetForm('loginFormRef')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "login",
        data() {
            return {
                // 表单数据绑定对象
                loginForm: {
                    username: 'admin',
                    password: '123456',
                },
                // 表单校验规则
                loginFormRules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入用户名密码', trigger: 'blur'},
                        {min: 3, max: 6, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ]
                },
            }
        },
        methods: {
           // 点击重置按钮,清空表单,并清除验证
           resetForm(formName){
               this.$refs[formName].resetFields();
           },
           // 对整个表单进行验证数据是否合法
           login(){
             this.$refs.loginFormRef.validate(valid => {
                 // 验证失败则不发送请求
                 if(!valid) return;
                 // 发送请求
                 this.$http.post("login", this.loginForm)
                   .then(res =>{
                       res = res.data;
                       // 登录失败
                       if(res.meta.status != 200){
                           this.$message.error(res.meta.msg)
                           return;
                       }
                       // 登录成功
                       this.$message.success(res.meta.msg)
                       // 1.将服务器返回的token保存在sessionStorage
                       window.sessionStorage.setItem("token", res.data.token);
                       // 2.跳转到主页
                       this.$router.push("/home");
                   }).catch(err => {
                     console.log("错误" + err);
               });
             })
           },
        },
    }
</script>

<style lang="less" scoped>
    .login_container {
        background-color: #2b4b6b;
        height: 100%;
    }

    .login_box {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);

        .avatar_box {
            height: 130px;
            width: 130px;
            border: 1px solid #eee;
            border-radius: 50%;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: pink;
            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
            }
        }
    }

    .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }

    .btns {
        display: flex;
        transform: translate(13%, 2%);
        text-align: center;
    }
</style>